package veetouch.home;

import veetouch.contact.ContactActivity;
import veetouch.gallery.GalleryActivity;
import veetouch.syncpc.R;
import veetouch.syncpc.VtAndroidSyncActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HomeActivity extends Activity
{	
	//////////////////////////////////////////////////////////
	//
	// DIALOG ID
	//
	//////////////////////////////////////////////////////////
	public static final int DIALOG_NOTICE = 0;		// Display notice dialog
	public static final int DIALOG_SHARE_LINK = 1;	// Display share link notice
	
	public AlertDialog dialog; 	// Popup dialog
    private String noticeText = "";
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        
        // Set Sync button
        Button button_sync = (Button) findViewById(R.id.Button_sync);
        button_sync.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View arg0) 
			{
				changePageToSync();
			}
		});
        
        // Set Toolbar button
        Button button_photo = (Button) findViewById(R.id.Button_photo);
        button_photo.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View arg0) 
			{
				changePageToGallery();
			}
		});
        
        Button button_link = (Button) findViewById(R.id.Button_link);
        button_link.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View arg0) 
			{
				showDialog(HomeActivity.DIALOG_SHARE_LINK);
			}
		});
        
        Button button_contact = (Button) findViewById(R.id.Button_contact);
        button_contact.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View arg0) 
			{
				changePageToContact();
			}
		});
    }
    
    @Override
	protected Dialog onCreateDialog(int id) 
	{
    	Builder builder;
		switch (id) {
			// noticeText
			case HomeActivity.DIALOG_NOTICE:
				builder = new AlertDialog.Builder(this);
				builder.setMessage(this.noticeText);
				builder.setPositiveButton("Ok",null);
				dialog = builder.create();
	            dialog.show();
	            break;
			case HomeActivity.DIALOG_SHARE_LINK:
				ClipboardManager clipboard;
				clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
				String temp = "";
				if(clipboard != null)
				{
					try
					{
						temp = (String) clipboard.getText();
					}
					catch(Exception e)
					{
						temp = "";
					}
				}
		    	final String link = temp;
		    	if(link == null || link == "")
		    	{
		    		builder = new AlertDialog.Builder(this);
		    		builder.setMessage("Please copy URL from web browser first.");
					builder.setPositiveButton("Ok",null);
					dialog = builder.create();
		            dialog.show();
		    	}
		    	else
		    	{
		    		builder = new AlertDialog.Builder(this);
					builder.setMessage("Share URL:\n"+link);
					builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
					{
						public void onClick(DialogInterface dialog, int which) 
		                {
							changePageToShareLink(link);
		                }
		            });
					builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		                public void onClick(DialogInterface dialog, int which) {
		                	
		                }
		            });
					dialog = builder.create();
		            dialog.show();
		    	}
				break;
		}
		return super.onCreateDialog(id);
	}
    
/*******************************************************
 * 
 *  Change Page to sub page
 * 
 *******************************************************/
    
    private void changePageToSync()
    {
    	Intent intent = new Intent(getApplicationContext(),VtAndroidSyncActivity.class);
    	intent.putExtra("SYNC_TYPE",VtAndroidSyncActivity.SYNC_TYPE_DOWNLOAD);
    	startActivity(intent);
    }
    
    /**
     * 
     * @param address
     * @param port
     */
    private void changePageToGallery()
	{
    	Intent intent = new Intent(getApplicationContext(),GalleryActivity.class);
		startActivity(intent);
	}
    
    /**
     * 
     */
    private void changePageToShareLink(String link)
    {
    	// Start Intent Sync
		Intent intent = new Intent(getApplicationContext(),VtAndroidSyncActivity.class);
    	intent.putExtra("SYNC_TYPE",VtAndroidSyncActivity.SYNC_TYPE_UPLOAD_LINK);
    	intent.putExtra("DATA",link);
		startActivity(intent);
    }
    
    private void changePageToContact()
    {
    	Intent intent = new Intent(getApplicationContext(),ContactActivity.class);
		startActivity(intent);
    }
}
